package com.zrf.testchatgpt.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Component
public class pythonServiceUtil {

    public JSONObject doPostTestThree(Map<String,Object> map) {
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        URI uri = null;
        CloseableHttpResponse response = null;
        Map<String,Object> result=new HashMap<>();
        try {
            // 将参数放入键值对类NameValuePair中,再放入集合中
            List<NameValuePair> params = new ArrayList<>();
            for (Map.Entry<String,Object>  entry:map.entrySet()
            ) {
                params.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
            }
            uri = new URIBuilder().setScheme("http").setHost("localhost").setPort(8000)
                    .setPath("/bat").setParameters(params).build();
            HttpGet httpPost = new HttpGet(uri);

            // 由客户端执行(发送)Post请求
            response = httpClient.execute(httpPost);
            // 从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();
            return JSON.parseObject(EntityUtils.toString(responseEntity));
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } finally {
            try {
                // 释放资源
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        result.put("code",500);
        result.put("msg","al服务器错误了");
        return new JSONObject(result);
    }
    public JSONObject stableChangeModel(Map<String,Object> map) {
        // 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        URI uri = null;
        CloseableHttpResponse response = null;
        Map<String,Object> result=new HashMap<>();
        try {
            // 将参数放入键值对类NameValuePair中,再放入集合中
            List<NameValuePair> params = new ArrayList<>();
            for (Map.Entry<String,Object>  entry:map.entrySet()
            ) {
                params.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
            }
            uri = new URIBuilder().setScheme("http").setHost("localhost").setPort(8000)
                    .setPath("/changeM").setParameters(params).build();
            HttpGet httpPost = new HttpGet(uri);

            // 由客户端执行(发送)Post请求
            response = httpClient.execute(httpPost);
            // 从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();
            return JSON.parseObject(EntityUtils.toString(responseEntity));
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } finally {
            try {
                // 释放资源
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        result.put("code",500);
        result.put("msg","al服务器错误了");
        return new JSONObject(result);
    }
    public  String doPost( Map<String, Object> map)  {
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        URI uri = null;
        CloseableHttpResponse response = null;
        try {
            // 将参数放入键值对类NameValuePair中,再放入集合中
            List<NameValuePair> params = new ArrayList<>();
            for (Map.Entry<String,Object>  entry:map.entrySet()
            ) {
                params.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
            }
            uri = new URIBuilder().setScheme("http").setHost("localhost").setPort(8000)
                    .setPath("/chat").setParameters(params).build();
            HttpGet httpPost = new HttpGet(uri);
            // 由客户端执行(发送)Post请求
            response = httpClient.execute(httpPost);
            // 从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();
            return EntityUtils.toString(responseEntity);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } finally {
            try {
                // 释放资源
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "服务器出错";
    }

    public JSONArray getModels(){
        // 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        URI uri = null;
        CloseableHttpResponse response = null;
        Map<String,Object> result=new HashMap<>();
        try {
            uri = new URIBuilder().setScheme("http").setHost("localhost").setPort(7860)
                    .setPath("/sdapi/v1/sd-models").build();
            HttpGet httpPost = new HttpGet(uri);
            response = httpClient.execute(httpPost);
            HttpEntity responseEntity = response.getEntity();
            return JSONArray.parseArray(EntityUtils.toString(responseEntity));
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } finally {
            try {
                // 释放资源
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        result.put("code",500);
        result.put("msg","al服务器错误了");
        return  JSONArray.parseArray(result.toString());

    }
    public Object getcurrentModel(){
        // 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        URI uri = null;
        CloseableHttpResponse response = null;
        Map<String,Object> result=new HashMap<>();
        try {
            uri = new URIBuilder().setScheme("http").setHost("localhost").setPort(7860)
                    .setPath("/sdapi/v1/options").build();
            HttpGet httpPost = new HttpGet(uri);
            response = httpClient.execute(httpPost);
            HttpEntity responseEntity = response.getEntity();
            JSONObject jsonObject = JSON.parseObject(EntityUtils.toString(responseEntity));
            Object sd_model_checkpoint = jsonObject.get("sd_model_checkpoint");
            return sd_model_checkpoint;
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } finally {
            try {
                // 释放资源
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        result.put("code",500);
        result.put("msg","al服务器错误了");
        return  JSON.parseObject(result.toString());

    }
}
